﻿using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace DebIt.Persistence.Model
{
    [Table(Name = "Loans")]
    public class LoanPo
    {
        #region variables

        [Column(IsVersion = true)]
        private Binary _version;
        private DateTime _timestamp;
        private readonly EntitySet<LoanItemPo> _loanItems = new EntitySet<LoanItemPo>();
        private readonly EntitySet<ClientLoanStatePo> _loanClients = new EntitySet<ClientLoanStatePo>();

        #endregion


        #region properties

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int LoanId { get; set; }

        [Column(CanBeNull = true)]
        public string Name { get; set; }

        [Column(CanBeNull = true)]
        public string Description { get; set; }

        [Column(CanBeNull = false)]
        public string Currency { get; set; }

        [Column(CanBeNull = false, DbType = "DateTime")]
        public DateTime Timestamp
        {
            get { return _timestamp.ToLocalTime(); }
            set
            {
                _timestamp = value.ToUniversalTime();
            }
        }

        [Association(Storage = "_loanItems", OtherKey = "LoanId", DeleteRule = "CASCADE")]
        public EntitySet<LoanItemPo> LoanItems
        {
            get { return _loanItems; }
            set { _loanItems.Assign(value); }
        }

        [Association(Storage = "_loanClients", OtherKey = "LoanId", DeleteRule = "CASCADE")]
        public EntitySet<ClientLoanStatePo> ClientLoanStates
        {
            get { return _loanClients; }
            set { _loanClients.Assign(value); }
        }

        #endregion

    }
}
